<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="上级id" prop="parentId">
        <el-tree-select
          v-model="formData.parentId"
          :data="baseMaterialTypeTree"
          :props="{...defaultProps, label: 'typeName'}"
          check-strictly
          default-expand-all
          placeholder="请选择上级id"
        />
      </el-form-item>
      <el-form-item label="类型代码" prop="typeCode">
        <el-input v-model="formData.typeCode" placeholder="请输入类型代码" />
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="formData.typeName" placeholder="请输入类型名称" />
      </el-form-item>
      <el-form-item label="钉条" prop="nailStrip">
        <el-input v-model="formData.nailStrip" placeholder="请输入钉条" />
      </el-form-item>
      <el-form-item label="加分" prop="extraPoints">
        <el-input v-model="formData.extraPoints" placeholder="请输入加分" />
      </el-form-item>
      <el-form-item label="路径" prop="path">
        <el-input v-model="formData.path" placeholder="请输入路径" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="包装数" prop="packageQuantity">
        <el-input v-model="formData.packageQuantity" placeholder="请输入包装数" />
      </el-form-item>
      <el-form-item label="下拉框中展示" prop="isDisplay">
        <el-radio-group v-model="formData.isDisplay">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="参数1" prop="parameterOne">
        <el-input v-model="formData.parameterOne" placeholder="请输入参数1" />
      </el-form-item>
      <el-form-item label="参数2" prop="parameterTwo">
        <el-input v-model="formData.parameterTwo" placeholder="请输入参数2" />
      </el-form-item>
      <el-form-item label="参数3" prop="parameterThree">
        <el-input v-model="formData.parameterThree" placeholder="请输入参数3" />
      </el-form-item>
      <el-form-item label="参数4" prop="parameterFour">
        <el-input v-model="formData.parameterFour" placeholder="请输入参数4" />
      </el-form-item>
      <el-form-item label="参数5" prop="parameterFive">
        <el-input v-model="formData.parameterFive" placeholder="请输入参数5" />
      </el-form-item>
      <el-form-item label="参数6" prop="parameterSix">
        <el-input v-model="formData.parameterSix" placeholder="请输入参数6" />
      </el-form-item>
      <el-form-item label="参数7" prop="parameterSeven">
        <el-input v-model="formData.parameterSeven" placeholder="请输入参数7" />
      </el-form-item>
      <el-form-item label="参数8" prop="parameterEight">
        <el-input v-model="formData.parameterEight" placeholder="请输入参数8" />
      </el-form-item>
      <el-form-item label="参数9" prop="parameterNine">
        <el-input v-model="formData.parameterNine" placeholder="请输入参数9" />
      </el-form-item>
      <el-form-item label="参数10" prop="parameterTen">
        <el-input v-model="formData.parameterTen" placeholder="请输入参数10" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseMaterialTypeApi, BaseMaterialTypeVO } from '@/api/erp/basematerialtype'
import { defaultProps, handleTree } from '@/utils/tree'

/** ERP基础资料-材质类型 表单 */
defineOptions({ name: 'BaseMaterialTypeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  parentId: undefined,
  typeCode: undefined,
  typeName: undefined,
  nailStrip: undefined,
  extraPoints: undefined,
  path: undefined,
  remark: undefined,
  packageQuantity: undefined,
  isDisplay: undefined,
  parameterOne: undefined,
  parameterTwo: undefined,
  parameterThree: undefined,
  parameterFour: undefined,
  parameterFive: undefined,
  parameterSix: undefined,
  parameterSeven: undefined,
  parameterEight: undefined,
  parameterNine: undefined,
  parameterTen: undefined
})
const formRules = reactive({
  parentId: [{ required: true, message: '上级id不能为空', trigger: 'blur' }],
  typeCode: [{ required: true, message: '类型代码不能为空', trigger: 'blur' }],
  nailStrip: [{ required: true, message: '钉条不能为空', trigger: 'blur' }],
  extraPoints: [{ required: true, message: '加分不能为空', trigger: 'blur' }],
  isDisplay: [{ required: true, message: '下拉框中展示不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const baseMaterialTypeTree = ref() // 树形结构

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseMaterialTypeApi.getBaseMaterialType(id)
    } finally {
      formLoading.value = false
    }
  }
  await getBaseMaterialTypeTree()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseMaterialTypeVO
    if (formType.value === 'create') {
      await BaseMaterialTypeApi.createBaseMaterialType(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseMaterialTypeApi.updateBaseMaterialType(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    parentId: undefined,
    typeCode: undefined,
    typeName: undefined,
    nailStrip: undefined,
    extraPoints: undefined,
    path: undefined,
    remark: undefined,
    packageQuantity: undefined,
    isDisplay: undefined,
    parameterOne: undefined,
    parameterTwo: undefined,
    parameterThree: undefined,
    parameterFour: undefined,
    parameterFive: undefined,
    parameterSix: undefined,
    parameterSeven: undefined,
    parameterEight: undefined,
    parameterNine: undefined,
    parameterTen: undefined
  }
  formRef.value?.resetFields()
}

/** 获得ERP基础资料-材质类型树 */
const getBaseMaterialTypeTree = async () => {
  baseMaterialTypeTree.value = []
  const data = await BaseMaterialTypeApi.getBaseMaterialTypeList()
  const root: Tree = { id: 0, name: '顶级ERP基础资料-材质类型', children: [] }
  root.children = handleTree(data, 'id', 'parentId')
  baseMaterialTypeTree.value.push(root)
}
</script>